﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_HoaDon : System.Web.UI.Page
{
    TiemPhongDataContext db = new TiemPhongDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadListViewHoaDon();
        }
    }
    //loadListViewHoaDon()
    protected void loadListViewHoaDon()
    {
        var lvhd = db.HoaDons.ToList();
        this.ListViewHoaDon.DataSource = lvhd;
        this.ListViewHoaDon.DataBind();
        txtMaHD.Enabled = false;
        txtMaHD.Text = loadMaHD();
        loadComboMaBe();
        loadComboMaNVKT();
    }
    // tinh toan MaHD
    protected string loadMaHD()
    {
        string cid = "";
        var selectID = (from u in db.HoaDons select u.MaHD).Count() + 1;
        cid = selectID.ToString();
        cid = "HD" + cid;
        return cid;
    }
    // set lat textbox
    protected void ClearControl()
    {
        //txtMaHD.Text = string.Empty;
        cbMaBe.Enabled = true;
        cbMaNVKT.Enabled = true;
        txtNgayLap.Text = string.Empty;
        txtTongTien.Text = string.Empty;
    }
    // hàm tải comboboxMaBe    
    protected void loadComboMaBe()
    {
        var listMB = from v in db.ThongTinBes select v;
        cbMaBe.DataValueField = "MaBe";
        cbMaBe.DataTextField = "MaBe";
        cbMaBe.DataSource = listMB.ToList();
        cbMaBe.DataBind();
        cbMaBe.Items.Insert(0, "--Select--");        
    }
    // hàm tải comboboxMaNVKT
    protected void loadComboMaNVKT()
    {
        var listNVKT = from v in db.NhanViens select v;
        cbMaNVKT.DataValueField = "MaNV";
        cbMaNVKT.DataTextField = "MaNV";
        cbMaNVKT.DataSource = listNVKT.ToList();
        cbMaNVKT.DataBind();
        cbMaNVKT.Items.Insert(0, "--Select--");
    }
    // ham tai du lieu len form sua
    protected void ListViewHoaDon_OnItemCommand(object sender, ListViewCommandEventArgs e)
    {
        // khai bao bien lay MaHD
        var maHD = e.CommandArgument.ToString();
        if (e.CommandName == "Delete")
        {
            var deleteHD = db.HoaDons.FirstOrDefault(c => c.MaHD.Equals(maHD));
            db.HoaDons.DeleteOnSubmit(deleteHD);
            db.SubmitChanges();
            // thong bao
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Xoa Thanh Cong";
            // tai lai lv
            loadListViewHoaDon();
            ClearControl();
        }
        if (e.CommandName == "Update")
        {
            var updateHD = db.HoaDons.FirstOrDefault(c => c.MaHD.Equals(maHD));
            btnSua.Enabled = true;
            btnThem.Enabled = false;

            txtMaHD.Text = updateHD.MaHD;
            cbMaNVKT.SelectedValue = updateHD.MaNVKT;
            cbMaBe.SelectedValue = updateHD.MaBe;
            txtNgayLap.Text = updateHD.NgayLap.HasValue ? Convert.ToDateTime(updateHD.NgayLap).ToString("d") : String.Empty;
            txtTongTien.Text = updateHD.TongTien.ToString();
        }
    }
    protected void ListViewHoaDon_OnItemDeleting(object sender, ListViewDeleteEventArgs e){}
    protected void ListViewHoaDon_OnItemUpdating(object sender, ListViewUpdateEventArgs e) { }
    // nhấn nút thêm
    protected void btnThem_Click(object sender, EventArgs e)
    {
        try
        {
            HoaDon _hd = new HoaDon();

            _hd.MaHD = txtMaHD.Text;
            _hd.MaNVKT = cbMaNVKT.SelectedValue;
            _hd.MaBe = cbMaBe.SelectedValue;
            //_hd.NgayLap = DateTime.ParseExact(this.txtNgayLap.Text, "dd/MM/yy",null);
            _hd.NgayLap = Convert.ToDateTime(txtNgayLap.Text);
            _hd.TongTien = Convert.ToDecimal(txtTongTien.Text);

            db.HoaDons.InsertOnSubmit(_hd);
            db.SubmitChanges();

            messageKetQua.Enabled = true;
            messageKetQua.Text = "Them thanh cong!";

            loadListViewHoaDon();
            ClearControl();
        }
        catch(Exception ex)
        {
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Loi: " + ex.Message;
        }
    }
    // nhấn nút sửa
    protected void btnSua_Click(object sender, EventArgs e)
    {
        try
        {
            var sss = db.HoaDons.Where(k => k.MaHD == txtMaHD.Text).SingleOrDefault();
            if (sss != null)
            {
                sss.MaNVKT = cbMaNVKT.SelectedValue;
                sss.MaBe = cbMaBe.SelectedValue;
                sss.NgayLap = Convert.ToDateTime(txtNgayLap.Text);
                sss.TongTien = Convert.ToDecimal(txtTongTien.Text);
                db.SubmitChanges();
                //
                btnThem.Enabled = true;
                btnSua.Enabled = false;
                //
                messageKetQua.Enabled = true;
                messageKetQua.Text = "Sua thanh cong";
                //
                loadListViewHoaDon();
                ClearControl();
            }
        }
        catch(Exception ek)
        {
            messageKetQua.Enabled = true;
            messageKetQua.Text = "Loi: " + ek.Message;
        }
    }
}